package cn.schoolwow.quickflow.service.flowContext.option;

import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.domain.FlowExecutorConfig;
import cn.schoolwow.quickflow.domain.FlowExecutorRootConfig;
import com.alibaba.fastjson.JSONArray;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class FlowContextOptionImpl implements FlowContextOption{
    private FlowExecutorConfig flowExecutorConfig;

    public FlowContextOptionImpl(FlowExecutorConfig flowExecutorConfig) {
        this.flowExecutorConfig = flowExecutorConfig;
    }

    @Override
    public FlowContext startDebugMode() {
        flowExecutorConfig.flowExecutorRootConfig.flowConfig.flowConfigSwitcherContext.debug = true;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext stopDebugMode() {
        flowExecutorConfig.flowExecutorRootConfig.flowConfig.flowConfigSwitcherContext.debug = false;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext checkRepeatKeyName(boolean checkRepeatKeyName) {
        flowExecutorConfig.flowExecutorRootConfig.flowConfig.flowConfigSwitcherContext.checkRepeatKeyName = checkRepeatKeyName;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public long getId() {
        return flowExecutorConfig.flowExecutorRootConfig.id;
    }

    @Override
    public FlowContext setFlowLogId(long flowLogId) {
        flowExecutorConfig.flowExecutorRootConfig.id = flowLogId;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext printTrace(boolean printTrace) {
        flowExecutorConfig.printTrace = printTrace;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext ignoreBroken(boolean ignoreBroken) {
        flowExecutorConfig.ignoreBroken = ignoreBroken;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext ignoreException(boolean ignoreException) {
        flowExecutorConfig.ignoreException = ignoreException;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public FlowContext remark(String remark) {
        flowExecutorConfig.getRemarkBuilder().append("|"+remark);
        return flowExecutorConfig.flowContext;
    }

    @Override
    public String getFlowName() {
        return flowExecutorConfig.name;
    }

    @Override
    public String getCurrentFlowName() {
        return flowExecutorConfig.currentFlowName;
    }

    @Override
    public JSONArray getFlowArray() {
        return flowExecutorConfig.flowExecutorRootConfig.flowArray;
    }

    @Override
    public String getFlowTrace() {
        return flowExecutorConfig.flowExecutorRootConfig.printTraceBuilder.toString();
    }

    @Override
    public Exception getFlowException() {
        return flowExecutorConfig.flowExecutorRootConfig.exception;
    }

    @Override
    public String getBrokenReason() {
        return flowExecutorConfig.flowExecutorRootConfig.brokenReason;
    }

    @Override
    public FlowExecutorConfig getFlowExecutorConfig() {
        return flowExecutorConfig;
    }

    @Override
    public FlowExecutorRootConfig getFlowExecutorRootConfig() {
        return flowExecutorConfig.flowExecutorRootConfig;
    }

    @Override
    public QuickFlow getQuickFlow() {
        return flowExecutorConfig.flowExecutorRootConfig.quickFlow;
    }

    @Override
    public String getRecord() {
        return flowExecutorConfig.flowExecutorRootConfig.recordBuilder.toString();
    }

    @Override
    public FlowContext enableLog(boolean enableLog){
        flowExecutorConfig.flowExecutorRootConfig.enableUserLog = enableLog;
        return flowExecutorConfig.flowContext;
    }

    @Override
    public String getLog() {
        return flowExecutorConfig.flowExecutorRootConfig.userLog.toString();
    }

    @Override
    public FlowContext log(String logContent, Object... parameters) {
        if(!flowExecutorConfig.flowExecutorRootConfig.enableUserLog){
            return flowExecutorConfig.flowContext;
        }
        if(null!=parameters&&parameters.length>0){
            for(Object parameter:parameters){
                logContent = logContent.replaceFirst("\\{}", parameter.toString());
            }
        }
        flowExecutorConfig.flowExecutorRootConfig.userLog.append("["+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))+"]"+logContent+"\r\n");
        return flowExecutorConfig.flowContext;
    }
}
